package com.yd.controller.system;


import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yd.controller.yd.YD_Sys;
import com.yd.controller.yd.YD_Users;
import com.yd.pojo.Balance;
import com.yd.service.BalanceService;
import com.yd.service.OrdersService;
import com.yd.tool.UtsReg;



@Controller
public class BalanceControllerSys {

	@Resource
	private BalanceService balanceService;
	@Resource
	private OrdersService ordersService;
	
	//余额表list
	@RequestMapping("/sys/balance/list")
	@ResponseBody
	public String sys_balance_list(HttpServletRequest request) throws MalformedURLException, IOException {
		JSONObject jsonObject = new JSONObject();
		
		String token="";
		String so = "";
		String page = "";
		String size = "";
		
		if (UtsReg.getStringKB(request.getParameter("token"))) {token = request.getParameter("token").trim();} else {jsonObject.put("msg", "token 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringKB(request.getParameter("so"))) {so = request.getParameter("so").trim();}
		if (UtsReg.getIntXl(request.getParameter("page"))) {page = request.getParameter("page").trim();}else{page = "1";}
		if (UtsReg.getStringKB(request.getParameter("size"))) {size = request.getParameter("size").trim();}else{size = "10";}
		
		//验证token
		JSONObject yd_users = YD_Sys.getshidByToken(token);
		if (yd_users.getString("error").equals("1")) {jsonObject.put("msg", yd_users.getString("msg"));jsonObject.put("error", "1");return jsonObject.toString();}
		JSONObject users_token = JSONObject.parseObject(yd_users.getString("data"));
		
		PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(size));
        List<Balance> list = null;
		EntityWrapper<Balance> ew = new EntityWrapper<Balance>();
		if (so.length() > 0) {ew.eq("usersid", so);}
		list = balanceService.selectList(ew.orderBy("id", false));
        PageInfo<Balance> pages = new PageInfo<Balance>(list);

		jsonObject.put("data", pages);
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();
	}
	
	//修改
	@RequestMapping("/sys/balance/up")
	@ResponseBody
	public String sys_balance_up(HttpServletRequest request) throws MalformedURLException, IOException {
		JSONObject jsonObject = new JSONObject();
		
		String token="";
        String id="";
		String usersid="";
		String usersname="";
		String usersimg="";
		String yingjiekuan="";
		String shijiyingjiekuan="";
		String yijiekuan="";
		
		if (UtsReg.getStringKB(request.getParameter("token"))) {token = request.getParameter("token").trim();} else {jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if(UtsReg.getStringKB(request.getParameter("id"))){id=request.getParameter("id").trim();}else{jsonObject.put("msg", "来个id");jsonObject.put("error", "1");return jsonObject.toString();}
		if(UtsReg.getStringKB(request.getParameter("usersid"))){usersid=request.getParameter("usersid").trim();}
		if(UtsReg.getStringKB(request.getParameter("usersname"))){usersname=request.getParameter("usersname").trim();}
		if(UtsReg.getStringKB(request.getParameter("usersimg"))){usersimg=request.getParameter("usersimg").trim();}
		if(UtsReg.getDouble(request.getParameter("yingjiekuan"))){yingjiekuan=request.getParameter("yingjiekuan").trim();}
		if(UtsReg.getDouble(request.getParameter("shijiyingjiekuan"))){shijiyingjiekuan=request.getParameter("shijiyingjiekuan").trim();}
		if(UtsReg.getDouble(request.getParameter("yijiekuan"))){yijiekuan=request.getParameter("yijiekuan").trim();}
		
		//验证token
		JSONObject yd_users = YD_Sys.getshidByToken(token);
		if (yd_users.getString("error").equals("1")) {jsonObject.put("msg", yd_users.getString("msg"));jsonObject.put("error", "1");return jsonObject.toString();}
		JSONObject users_token = JSONObject.parseObject(yd_users.getString("data"));
		
		Balance balance=new Balance();
		balance.setId(Integer.valueOf(id));
		balance.setUsersid(usersid);
		balance.setUsersimg(usersimg);
		balance.setUsersname(usersname);
		balance.setYijiekuan(yijiekuan);;
		balance.setYingjiekuan(yingjiekuan);
		balance.setShijiyingjiekuan(shijiyingjiekuan);
		
		if(!balanceService.updateById(balance)){
			jsonObject.put("msg", "失败");jsonObject.put("error", "1");return jsonObject.toString();
		}
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		return jsonObject.toString();


	}
	//删除
	@RequestMapping("/sys/balance/delbyid")
	@ResponseBody
	public String sys_balance_delbyid(HttpServletRequest request) throws MalformedURLException, IOException {
		JSONObject jsonObject = new JSONObject();
		
		String token="";
        String id="";
		
        if (UtsReg.getStringKB(request.getParameter("token"))) {token = request.getParameter("token").trim();} else {jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if(UtsReg.getStringKB(request.getParameter("id"))){id=request.getParameter("id").trim();}else{jsonObject.put("msg", "来个id");jsonObject.put("error", "1");return jsonObject.toString();}
		
		//验证token
		JSONObject yd_users = YD_Sys.getshidByToken(token);
		if (yd_users.getString("error").equals("1")) {jsonObject.put("msg", yd_users.getString("msg"));jsonObject.put("error", "1");return jsonObject.toString();}
		JSONObject users_token = JSONObject.parseObject(yd_users.getString("data"));
		
		if(balanceService.selectCount(new EntityWrapper<Balance>().eq("id", id))<1){
			jsonObject.put("msg", "数据不存在");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		boolean ei=balanceService.deleteById(Integer.valueOf(id));
		if(!ei){
			jsonObject.put("error", "1");jsonObject.put("msg", "失败");return jsonObject.toString();
		}
		
		
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		return jsonObject.toString();


	}
	
}
